﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Xml;

namespace blackhouse.Payments {
    /// <summary>
    /// Summary description for PaymentXml
    /// </summary>
    public class PaymentXml : IHttpHandler {

        public void ProcessRequest(HttpContext context) {
            int posId = Int32.Parse(context.Request["pos_id"]);
            string sessionId = context.Request["session_id"];
            PaymentTransaction transaction = PaymentTransaction.Find(posId, sessionId);
            if (transaction != null) {
                XmlDocument doc = new XmlDocument();
                XmlElement xmlResponse = doc.CreateElement("response");
                doc.AppendChild(xmlResponse);
                XmlElement xmlStatus = doc.CreateElement("status");
                xmlResponse.AppendChild(xmlStatus);
                xmlStatus.InnerText = "OK";
                XmlElement xmlTrans = doc.CreateElement("trans");
                xmlResponse.AppendChild(xmlTrans);
                this.AddXmlElement(xmlTrans, "id", "7");
                this.AddXmlElement(xmlTrans, "pos_id", transaction.Configuration.PosID.ToString());
                this.AddXmlElement(xmlTrans, "session_id", transaction.UserSessionId);
                this.AddXmlElement(xmlTrans, "order_id", transaction.OrderId.ToString());
                this.AddXmlElement(xmlTrans, "amount", Convert.ToInt32(transaction.ChargeValue * 100).ToString());
                this.AddXmlElement(xmlTrans, "status", transaction.Status.StatusId.ToString());
                //this.AddXmlElement(xmlTrans, "status", "99");
                this.AddXmlElement(xmlTrans, "pay_type", transaction.PaymentType.PaymentKey.ToString());
                this.AddXmlElement(xmlTrans, "pay_gw_name", "");
                this.AddXmlElement(xmlTrans, "desc", transaction.Description);
                this.AddXmlElement(xmlTrans, "desc2", transaction.DescriptionExtension);
                this.AddXmlElement(xmlTrans, "create", transaction.CreatedDate.ToString("yyyy-MM-ddHH:mm:ss", CultureInfo.GetCultureInfo("pl-PL")));
                this.AddXmlElement(xmlTrans, "init", transaction.BeginDate.ToString("yyyy-MM-ddHH:mm:ss", CultureInfo.GetCultureInfo("pl-PL")));
                this.AddXmlElement(xmlTrans, "sent", transaction.SentDate.ToString("yyyy-MM-ddHH:mm:ss", CultureInfo.GetCultureInfo("pl-PL")));
                this.AddXmlElement(xmlTrans, "recv", transaction.ReceiveDate.ToString("yyyy-MM-ddHH:mm:ss", CultureInfo.GetCultureInfo("pl-PL")));
                this.AddXmlElement(xmlTrans, "cancel", transaction.CancelDate.ToString("yyyy-MM-ddHH:mm:ss", CultureInfo.GetCultureInfo("pl-PL")));
                this.AddXmlElement(xmlTrans, "auth_fraud", "");
                this.AddXmlElement(xmlTrans, "ts", transaction.Ts);
                this.AddXmlElement(xmlTrans, "sig", this.getSig(
                    transaction.Configuration.PosID,
                    transaction.UserSessionId,
                    transaction.OrderId,
                    transaction.Status.StatusId,
                    //99,
                    Convert.ToInt32(transaction.ChargeValue * 100),
                    transaction.Description,
                    transaction.Ts,
                    transaction.Configuration.KeyIn
                    ));
                context.Response.ContentEncoding = System.Text.Encoding.UTF8; 
                doc.Save(context.Response.Output);
            }
        }

        private string getSig(int posId, string sessionId, int orderId, int statusId, int amount, string desc, string ts, string key2) {
            string s = posId + sessionId + (orderId > 0 ? orderId.ToString() : "") + statusId + amount + desc + ts + key2;
            return s.GetMd5();
        }

        private void AddXmlElement(XmlElement xmlParentElement, string elementName, string value) {
            XmlElement xmlEl = xmlParentElement.OwnerDocument.CreateElement(elementName);
            xmlParentElement.AppendChild(xmlEl);
            xmlEl.InnerText = value;
        }

        public bool IsReusable {
            get {
                return false;
            }
        }
    }
}